package cn.com.inossem.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;

import java.util.ArrayList;
import java.util.List;

import cn.com.inossem.activity.R;
import cn.com.inossem.model.HourApplyBean;
import cn.com.inossem.model.ProjectApplyViewHolder;

/**
 * 工时列表adapter
 * Created by Mickey.Li on 2017-8-8.
 */
public class ApplyHourAdapter extends RecyclerView.Adapter<ProjectApplyViewHolder> {
	private LayoutInflater layoutInflater;
	private View.OnClickListener listener;
	private int orderStatus;
	private List<HourApplyBean.HourItemBean> items = new ArrayList<>();
	private boolean isEdit = false;
	public SparseBooleanArray selArray;

	public ApplyHourAdapter(Context context, View.OnClickListener listener, int orderStatus) {
		this.layoutInflater = LayoutInflater.from(context);
		this.listener = listener;
		this.orderStatus = orderStatus;
		selArray = new SparseBooleanArray();
	}

	@Override
	public ProjectApplyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		return new ProjectApplyViewHolder(layoutInflater.inflate(R.layout.item_apply_hour, null));
	}

	@Override
	public void onBindViewHolder(ProjectApplyViewHolder holder, final int position) {
		holder.itemApplyCb.setVisibility(isEdit ? View.VISIBLE : View.GONE);
		holder.itemApplyCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				selArray.put(position, isChecked);
			}
		});
		holder.itemApplyCb.setChecked(false);
		holder.itemApply2.setText("" + (position + 1));
		holder.itemApply1.setText(items.get(position).datStr);
		holder.itemApply3.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
		holder.itemApply3.setTag(position);
		holder.itemApply3.setText("" + items.get(position).hours);
		if (orderStatus == -1 || orderStatus == 2) {
			holder.itemApply3.setOnClickListener(listener);
		} else {
			holder.itemApply3.setCompoundDrawables(null, null, null, null);
		}
	}

	@Override
	public int getItemCount() {
		return items.size();
	}

	public void changeHourItem(int position, int hours) {
		items.get(position).hours = hours;
		notifyDataSetChanged();
	}

	public void setHourList(List<HourApplyBean.HourItemBean> itemList) {
		items = new ArrayList<>();
		items.addAll(itemList);
		notifyDataSetChanged();
	}

	public void changeEditStatus(boolean isEdit) {
		this.isEdit = isEdit;
		this.notifyDataSetChanged();
	}
}
